package com.inossem.warehousestorage_inossem.activity.inventory.chuangyou;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.inventory.extre.InventoryExtre;
import com.inossem.warehousestorage_inossem.adapter.inventory.chuangyou.InventoryPositionsAdapter;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventoryPositionsBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.chuangyou.InventoryPositionListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;

import java.util.List;

/**
 * 库存盘点 仓位列表
 *
 * @author 詹建宇
 * @time on 2018/3/30 16:59
 * @email jianyu.zhan@inossem.com
 */
public class InventoryPositionListActivity extends BaseActivity implements BaseInteface, View.OnClickListener {

    private String stocktake_id;//盘点凭证ID

    private TextView title;
    private GridView gridView;
    private TextView temporarilyTv;//暂存
    private TextView nextTv;//下一步

    private InventoryPositionsBean bean;
    private List<InventoryPositionsBean.PositionBodyBean.PositionListBean> list;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_inventory_position_list);

        initView(this);
        getExtra(this);
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        gridView = (GridView) activity.findViewById(R.id.gridView);
        temporarilyTv = (TextView) activity.findViewById(R.id.temporarilyTv);
        nextTv = (TextView) activity.findViewById(R.id.nextTv);

        temporarilyTv.setOnClickListener(this);
        nextTv.setOnClickListener(this);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                InventoryPositionsBean.PositionBodyBean.PositionListBean bean = list.get(i);
                Intent intent = new Intent(activity, InventoryMatsActivity.class);
                intent.putExtra(InventoryExtre.STOCKTAKE_ID, stocktake_id);
                intent.putExtra(InventoryExtre.POSITION_ID, bean.getPosition_id());
                activity.startActivity(intent);
            }
        });
    }

    @Override
    public void initData(Activity activity) {
        title.setText("库存盘点");
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_result_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getData(activity);
    }

    private void getExtra(Activity activity) {
        stocktake_id = activity.getIntent().getStringExtra(InventoryExtre.STOCKTAKE_ID);
    }

    private void getData(Activity activity) {
        InventoryPositionListRequest.request(activity, stocktake_id, new GsonBeanListener<InventoryPositionsBean>() {
            @Override
            public void onSuccess(InventoryPositionsBean bean) {
                InventoryPositionListActivity.this.bean = bean;
                list = bean.getBody().getPosition_list();
                InventoryPositionsAdapter adapter = new InventoryPositionsAdapter(activity, list);
                gridView.setAdapter(adapter);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.temporarilyTv://暫存
                finish();
                break;
            case R.id.nextTv://下一步

                String shouldNum = String.valueOf(bean.getBody().getPosition_list().size());
                int num = 0;//已經盤點個數
                for (InventoryPositionsBean.PositionBodyBean.PositionListBean b : bean.getBody().getPosition_list()) {
                    int mat_count = Integer.parseInt(b.getMat_count());
                    int stocktake_count = Integer.parseInt(b.getStocktake_count());
                    if (mat_count == stocktake_count) {
                        num++;
                    }
                }
                String alreadyNum = String.valueOf(num);

                Intent intent = new Intent(InventoryPositionListActivity.this, InventoryCommitActivity.class);
                intent.putExtra(InventoryExtre.STOCKTAKE_ID, stocktake_id);
                intent.putExtra(InventoryExtre.SHOULD_NUM, shouldNum);
                intent.putExtra(InventoryExtre.ALREADY_NUM, alreadyNum);
                InventoryPositionListActivity.this.startActivity(intent);
                break;
        }
    }

}
